package com.mingyizhudao.app.view.mine;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dixiang.framework.utils.AlertDialogs;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYBaseFragment;
import com.mingyizhudao.app.view.WebViewActivity;
import com.mingyizhudao.app.view.login.LoginActivity;
import com.mingyizhudao.app.view.login.LoginActivity2;

public class MineFragment extends MYBaseFragment{

	private TextView user_code;
	private ImageView avtar;
	private boolean  isLogin = false;
	private RelativeLayout view_loginout;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mine, container, false); 
		initView(view);
		return view;
	}

	private void initView(View view){
		view.findViewById(R.id.view_order).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_report).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_problem).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_about).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_call).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_gongyi).setOnClickListener(onClickListener);
		
		view.findViewById(R.id.state_1).setOnClickListener(onClickListener);
		view.findViewById(R.id.state_2).setOnClickListener(onClickListener);
		view.findViewById(R.id.state_3).setOnClickListener(onClickListener);
		view.findViewById(R.id.state_4).setOnClickListener(onClickListener);
		
		
		avtar = ( ImageView )view.findViewById(R.id.avtar);
		avtar.setOnClickListener(onClickListener);
		view_loginout = ( RelativeLayout )view.findViewById(R.id.view_loginout);
		view_loginout.setOnClickListener(onClickListener);

		user_code = ( TextView )view.findViewById(R.id.user_code);

	}


	@Override
	public void onResume() {

		String mobile =CommonUtils.getMobile( activity );
		String token =CommonUtils.getToken( activity );
		if( token!=null&&token.length()>0&&mobile!=null&&mobile.length()>0 )
		{
			isLogin = true;
			user_code.setText( mobile );
			view_loginout.setVisibility( View.VISIBLE );

		}else {
			user_code.setText( R.string.fm_personal_user_default_code );
			view_loginout.setVisibility( View.GONE );
		}
		super.onResume();
	}

	@Override
	public void initHeaderView() {
		//		clearTitle();
		//		setTopViewBg(R.color.white);
		//		setTitleViewValue( R.string.fragment_person_title, 0, R.color.actionbar_bg_color );
		setTopViewIsShow(false);
	}


	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.avtar:
				if( isLogin )
				{
					Toast.makeText( activity, "您已登录！", 0 ).show();
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}

				break;
			case R.id.view_order:
				if( isLogin )
				{
					IntentHelper.getInstance(activity).gotoActivity(OrderListActivity.class);
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}

				//IntentHelper.getInstance(activity).gotoActivity(OrderDetailActivity.class);

				break;
			case R.id.view_report:
				//IntentHelper.getInstance(activity).gotoActivity(ReportActivity.class);
				break;

			case R.id.view_problem:
				IntentHelper.getInstance(activity).gotoActivity(CommonProblemActivity.class);
				break;
			case R.id.view_about:
				IntentHelper.getInstance(activity).gotoActivity(AboutActivity.class);
				break;
			case R.id.view_call:
				IntentHelper.getInstance(activity).gotoActivity(CallActivity.class);
				break;
			case R.id.view_loginout:
				if(!isLogin){
					Toast.makeText( activity, "您还没登录！", 0 ).show();
				}else{
					AlertDialogs.getAlertDialog(activity).showAlertDialog("您确定要退出登录?", new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							isLogin = false;
							CommonUtils.setToken( activity, null );
							user_code.setText( R.string.fm_personal_user_default_code );
							view_loginout.setVisibility( View.GONE );
						}
					});
				}

				break;
			case R.id.view_gongyi:
				    Bundle bundle = new Bundle();
					bundle.putString(Constants.PARM_ACTION_URL, "http://m.mingyizhudao.com/mobile/event/view/page/mygy/header/0");
					bundle.putString(Constants.PARM_PAGE_TITLE, "名医公益");
					IntentHelper.getInstance(activity).gotoActivity(WebViewActivity.class, bundle);
				break;
				
			case R.id.state_1:
				if( isLogin )
				{
					goCommonList("1" ,"待支付");
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	
				
			case R.id.state_2:
				if( isLogin )
				{
					goCommonList("2","安排中");
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	
				
			case R.id.state_3:
				
				if( isLogin )
				{
					goCommonList("5","待确认");
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	
				
			case R.id.state_4:
				if( isLogin )
				{
					goCommonList("6","已完成");//6 和 8
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	

			default:
				break;
			}
		}
	};


	private void goCommonList(String type,String title) {
		Bundle bundle = new Bundle();
		bundle.putString("type", type);
		bundle.putString("title", title);
		IntentHelper.getInstance(activity).gotoActivity(OrderListCommonActivity.class, bundle);
	}
	
}
